package com.xsomnus.java_basic.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

/**
 * @author @xsomnus666_xiawenye★
 * @since 2019/7/19 0019 11:47
 * - 才需学也,学需静也/非淡泊无以明志，非宁静无以致远
 */
public class JDKProxy implements InvocationHandler {

    private Object target;

    public JDKProxy(Object target) {
        this.target = target;
    }

    @SuppressWarnings("all")
    public <T> T getProxyInstance(){
        return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object ret = method.invoke(target, args);
        System.out.println("代理类 " + proxy.getClass() + ", 代理方法： " + method.getName() + "参数： " + Arrays.toString(args));
        return ret;
    }
}
